<template>
	<view>
		<view class="cu-form-group">
			<view class="title">水体异味</view>
			<picker @change="ywChange" :value="ywIndex" :range="ywShow">
				<view class="picker">
					{{ywShow[ywIndex]}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">水体浑浊度</view>
			<picker @change="hzChange" :value="hzIndex" :range="hzShow">
				<view class="picker">
					{{hzShow[hzIndex]}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">空气污染程度</view>
			<picker @change="kqwrChange" :value="kqwrIndex" :range="kqwrShow">
				<view class="picker">
					{{kqwrShow[kqwrIndex]}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">空气有无异味</view>
			<picker @change="kqywChange" :value="kqywIndex" :range="kqywShow">
				<view class="picker">
					{{kqywShow[kqywIndex]}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">监测时间</view>
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
				<view class="picker">{{date}}</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">空气湿度</view>
			<input v-model="air_humidity" placeholder="请输入空气湿度" focus='true' name="input">%rh</input>
		</view>
		<view class="cu-form-group">
			<view class="title">气温</view>
			<input v-model="air_temperature" placeholder="请输入气温" focus='true' name="input">℃</input>
		</view>
		<view class="cu-form-group">
			<view class="title">水温</view>
			<input v-model="water_temperature" placeholder="请输入水温" focus='true' name="input">℃</input>
		</view>
		<view class="cu-form-group">
			<view class="title">监测员</view>
			<input v-model="monitor" maxlength="4" placeholder="请输入监测员名字" focus='true' name="input"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">监测员电话</view>
			<input v-model="monitor_phone" maxlength="11" type="number" placeholder="请输入监测员电话" focus='true' name="input"></input>
		</view>
	</view>
</template>

<script>
	function getDate(type) {
		const date = new Date();
	
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
	
		if (type === 'start') {
			year = year - 60;
		} else if (type === 'end') {
			year = year + 2;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
	
		return `${year}-${month}-${day}`;
	}
	export default {
		data() {
			return {
				ywShow:['无','明显','水体异味','水体异味强烈'],
				ywIndex:0,
				hzShow:['无','较浑浊','浑浊严重','非常浑浊'],
				hzIndex:0,
				kqwrShow:['无','轻度污染','中度污染','重度污染'],
				kqwrIndex:0,
				kqywShow:['无','有异味','异味较强','异味强烈'],
				kqywIndex:0,
				date: getDate({
					format: true
				}),
				startDate:getDate('start'),
				endDate:getDate('end'),
				time: '',//ui库
				air_humidity:'',//空气湿度
				air_temperature:"",//气温
				water_temperature:'',//水温
				monitor:"",//检测员
				monitor_phone:''//检测员电话
			}
		},
		onNavigationBarButtonTap() {
			uni.showModal({
				title:"提示",
				content:"确定要提交此次记录吗？",
				success: (res) => {
				if(res.confirm){
					this.addRecord();
				}	
				}
			})
		},
		methods: {
			 addRecord(){
				 if(!this.air_humidity){
					 uni.showToast({
					 	title:"请输入空气湿度",
						icon:"none"
					 })
					 return;
				 }
				  else if(!this.air_temperature){
				 					 uni.showToast({
				 					 	title:"请输入气温",
				 						icon:"none"
				 					 })
				 					 return;
				 }
				 else if (!this.water_temperature){
				 					 uni.showToast({
				 					 	title:"请输入水温",
				 						icon:"none"
				 					 })
				 					 return;
				 }
				 else if (!this.monitor){
				 					 uni.showToast({
				 					 	title:"请输入监测员",
				 						icon:"none"
				 					 })
				 					 return;
				 }
				 else if (!this.monitor_phone){
				 					 uni.showToast({
				 					 	title:"请输入监测员电话",
				 						icon:"none"
				 					 })
				 					 return;
				 }
				 uni.request({
				 	url: this.confige.webUrl+"/livestock/addGrowthEnvironment",
					header: {
						"X-Token":this.User.token
					},
				 	data:{
				 		air_humidity:this.air_humidity,
				 		air_odor:this.kqywShow[this.kqywIndex],
				 		air_pollution_degree:this.kqwrShow[this.kqwrIndex],
				 		air_temperature:this.air_temperature,
				 		farm_id:this.User.userInfo.farm_id,
				 		farm_name:this.User.userInfo.farm_name,
				 		monitor:this.monitor,
				 		monitor_phone:this.monitor_phone,
				 		monitoring_time:this.date,
				 		water_peculiar_smell:this.ywShow[this.ywIndex],
				 		water_temperature:this.water_temperature,
				 		water_turbidity:this.hzShow[this.ywIndex]
				 		
				 	},
				 	method:"POST",
					success: (res) => {
						console.log(res)
						if(res.data.code==200){
							uni.showToast({
								title:res.data.message
							});
							uni.switchTab({
								url:"../rear"
							})
						}
					}
				 })
				
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
				console.log(this.date)
			},
			//水体异味选中
			ywChange(e){
				this.ywIndex = e.detail.value;
			},
			//水体浑浊度
			hzChange(e){
				this.hzIndex = e.detail.value;
			},
			//空气污染程度
			kqwrChange(e){
				this.kqwrIndex = e.detail.value;
			},
			//空气有无异味
			kqywChange(e){
				this.kqywIndex = e.detail.value;
			}
			
			
		}
	}
</script>

<style lang="less" >
	@import '../../../common/animate.css';
	@import '../../../common/main.css';
	@import "../../../common/iconfont.css";
	@import "../../../common/colorui/icon.css";
	
</style>
